package org.qloudgen.borg.pages;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.UserBean;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;
import org.qloudgen.borg.pages.common.BlankPage;

public class UserPasswordPage extends BasePage{
	
	private static final long serialVersionUID = 1L;
	private PageParameters pm = new PageParameters() ;
	
	@SuppressWarnings({"unchecked","rawtypes"})
	public UserPasswordPage(){
		
		super();
		
		// get user info
		final UserBean user_ = DefaultImpl.getUserInfo(CommonValues.getUserNo());
		
		Form form = new Form("form"){
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				
				UserBean user = (UserBean) getDefaultModelObject();
				
				if (!user.getUserOldPassword().equals(user_.getUserPassword())){
					
					error(CommonValues.ERROR_OLD_PASSWORD);
					
				} else if (!user.getUserPassword().equals(user.getUserRePassword())){
					
					error(CommonValues.ERROR_TWO_PASSWORDS_MISMATCHER);
					
				} else if (user.getUserPassword().equals(user_.getUserPassword())){
					
					error(CommonValues.ERROR_SAME_PASSWORDS);
					
				} else {
					user.setUserNo(user_.getUserNo());
					DefaultImpl.modifyUserPassword(user);
					setResponsePage(BlankPage.class);
					
				}
			}
		};
		add(form);
		form.setDefaultModel(new CompoundPropertyModel(new UserBean()));	
		
		form.add(new PasswordTextField("userOldPassword"));
		form.add(new PasswordTextField("userPassword"));
		form.add(new PasswordTextField("userRePassword"));
		
		Button cancel = new Button ("cancel"){
			private static final long serialVersionUID = 1L;
			public void onSubmit() {
				setResponsePage(BlankPage.class);
			}
		};
		form.add(cancel);
		cancel.setDefaultFormProcessing(false);
	}

	public PageParameters getPm() {
		return pm;
	}

	public void setPm(PageParameters pm) {
		this.pm = pm;
	}
	
	
}
